<html>
<head><meta charset="utf-8"><title>Printing contents of a `Span` · t-compiler/help · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/index.html">t-compiler/help</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Printing.20contents.20of.20a.20.60Span.60.html">Printing contents of a `Span`</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="169575673"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Printing%20contents%20of%20a%20%60Span%60/near/169575673" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Dylan MacKenzie (ecstatic-morse) <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Printing.20contents.20of.20a.20.60Span.60.html#169575673">(Jul 03 2019 at 15:20)</a>:</h4>
<p>Sorry for the trivial question, but how do I get the contents of a <code>Span</code> as a string for debugging purposes?</p>



<a name="169576731"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Printing%20contents%20of%20a%20%60Span%60/near/169576731" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Dylan MacKenzie (ecstatic-morse) <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Printing.20contents.20of.20a.20.60Span.60.html#169576731">(Jul 03 2019 at 15:32)</a>:</h4>
<p>I only have access to a <code>TyCtxt</code>, not a <code>Session</code></p>



<a name="169579049"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Printing%20contents%20of%20a%20%60Span%60/near/169579049" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Dylan MacKenzie (ecstatic-morse) <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Printing.20contents.20of.20a.20.60Span.60.html#169579049">(Jul 03 2019 at 16:03)</a>:</h4>
<p>Ah, I wanted <code>tcx.gcx.sess.source_map().span_to_string(sp)</code></p>



<a name="169579659"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Printing%20contents%20of%20a%20%60Span%60/near/169579659" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> varkor <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Printing.20contents.20of.20a.20.60Span.60.html#169579659">(Jul 03 2019 at 16:11)</a>:</h4>
<p><span class="user-mention" data-user-id="118594">@Dylan MacKenzie</span>: you can access <code>tcx.sess</code> directly, without going via <code>gcx</code></p>



<a name="169579738"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Printing%20contents%20of%20a%20%60Span%60/near/169579738" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> varkor <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Printing.20contents.20of.20a.20.60Span.60.html#169579738">(Jul 03 2019 at 16:12)</a>:</h4>
<p>(more often you want <code>tcx.sess.source_map().span_to_snippet(span)</code>)</p>



<a name="169580105"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Printing%20contents%20of%20a%20%60Span%60/near/169580105" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Dylan MacKenzie (ecstatic-morse) <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Printing.20contents.20of.20a.20.60Span.60.html#169580105">(Jul 03 2019 at 16:16)</a>:</h4>
<p><code>span_to_snippet</code> is indeed what I wanted</p>



<a name="169580275"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Printing%20contents%20of%20a%20%60Span%60/near/169580275" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Dylan MacKenzie (ecstatic-morse) <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Printing.20contents.20of.20a.20.60Span.60.html#169580275">(Jul 03 2019 at 16:18)</a>:</h4>
<p>And I now see the <code>Deref</code> impl</p>



<a name="169580277"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Printing%20contents%20of%20a%20%60Span%60/near/169580277" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Dylan MacKenzie (ecstatic-morse) <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Printing.20contents.20of.20a.20.60Span.60.html#169580277">(Jul 03 2019 at 16:18)</a>:</h4>
<p>Thanks <span class="user-mention" data-user-id="121053">@varkor</span></p>



<a name="169581213"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Printing%20contents%20of%20a%20%60Span%60/near/169581213" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> varkor <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Printing.20contents.20of.20a.20.60Span.60.html#169581213">(Jul 03 2019 at 16:30)</a>:</h4>
<p>glad I could help :)</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>